package group.igfd.manager.biz.service.impl;

import group.igfd.manager.biz.entity.OrderItemEntity;
import group.igfd.manager.biz.entity.OrdersEntity;
import group.igfd.manager.biz.service.OrderItemService;
import group.igfd.manager.biz.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import group.igfd.manager.biz.dao.OrderRecordDao;
import group.igfd.manager.biz.entity.OrderRecordEntity;
import group.igfd.manager.biz.service.OrderRecordService;
import org.springframework.transaction.annotation.Transactional;


@Service("orderRecordService")
public class OrderRecordServiceImpl extends ServiceImpl<OrderRecordDao, OrderRecordEntity> implements OrderRecordService {

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private OrdersService ordersService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<OrderRecordEntity> page = this.selectPage(
                new Query<OrderRecordEntity>(params).getPage(),
                new EntityWrapper<OrderRecordEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public boolean add(Integer type, String[] images,Integer orderItemId) throws Exception {
        String fileType;

        for (String image : images) {
            if (image.contains(".bmp") || image.contains(".jpg") || image.contains(".png")
                    || image.contains(".jpeg")|| image.contains(".gif")) {
                fileType = "image";
            } else {
                //fileType = "video";
                throw new Exception("上传小票或打包信息失败");
            }

            OrderRecordEntity orderRecordEntity = new OrderRecordEntity();

            orderRecordEntity.setOrderItemId(orderItemId);
            orderRecordEntity.setFile(image);
            orderRecordEntity.setFileType(fileType);
            orderRecordEntity.setTitlePicture(image);
            orderRecordEntity.setType(type);
            orderRecordEntity.setCreatedAt(new Date());
            orderRecordEntity.setUpdatedAt(new Date());
            boolean result = this.insert(orderRecordEntity);
            if (result == false) {
                throw new Exception("上传小票或打包信息失败");
            }
        }

        EntityWrapper<OrderItemEntity> orderItemEntityEntityWrapper = new EntityWrapper<>();
        orderItemEntityEntityWrapper.eq("id",orderItemId);
        OrderItemEntity orderItemEntity = orderItemService.selectOne(orderItemEntityEntityWrapper);

        Integer orderId = orderItemEntity.getOrderId();

        EntityWrapper<OrderItemEntity> ordersEntityEntityWrapper = new EntityWrapper<>();
        ordersEntityEntityWrapper.eq("order_id",orderId);
        List<OrderItemEntity> orderItemEntities = orderItemService.selectList(ordersEntityEntityWrapper);

        boolean uploadAll = true;
        for (OrderItemEntity orderItem :orderItemEntities) {
            EntityWrapper<OrderRecordEntity> orderRecordEntityEntityWrapper = new EntityWrapper<>();
            orderRecordEntityEntityWrapper.eq("order_item_id",orderItem.getId());
            orderRecordEntityEntityWrapper.eq("type",type);
            int count = this.selectCount(orderRecordEntityEntityWrapper);
            if (count == 0) {
                uploadAll = false;
                break;
            }
        }

        if (uploadAll) {
            OrdersEntity ordersEntity = new OrdersEntity();

            ordersEntity.setId(orderId);
            if (type == 1) {
                //上传小票
                ordersEntity.setStatus(9);
            } else if (type == 2) {
                //上传打包
                ordersEntity.setStatus(10);
            }
            boolean b = ordersService.updateById(ordersEntity);
            if (b == false) {
                throw new Exception("上传小票或打包信息失败");
            }
        }

        return true;
    }

}
